class addUser {

    constructor(addUserElementSelector, changeStatusElementSelector, resetPassWordElementSelector, deleteUserElementSelector, editUserElementSelector) {
        this.editUserEle = $(editUserElementSelector);
        this.deleteUserEle = $(deleteUserElementSelector);
        this.addUserEle = $(addUserElementSelector);
        this.changeStatusEle = $(changeStatusElementSelector);
        this.changePassworEle = $(resetPassWordElementSelector);
        this.user_type = $('#user_type').attr('content');

        this.changePasswordHtml = `<form>
                      <div class="form-group">
                        <label for="exampleInputEmail1">新密码</label>
                        <input type="email" class="form-control bg-light" id="resetpwd" aria-describedby="emailHelp" placeholder="请输入新密码 ">
                      </div>           
                    </form>`;

        if(this.user_type === 'cms'){
            this.urls = {
                edit:'/cms/editcms/',
                reset:'/cms/cmsreset/',
                delete_user:'/cms/deletecmsuser/',
                edit_user:'/cms/editcmsuser/'
            }
        }else{
            this.urls = {
                edit:'/cms/edit/',
                reset:'/cms/reset/',
                delete_user:'/cms/deleteuser/',
                edit_user:'/cms/edituser/',
            }
        }
    }

    addEventListener() {
        let that = this;
        $('#add-submit').on('click',function () {
            let username = $('#username').val();
                        let password = $('#password').val();
                        let password1 = $('#password1').val();
                        let point = $('#point').val();
                        let csrf = $('#csrf').attr('content');
                        if (username.length < 1 || username.length > 50) {
                            $('#alertTiele').text('error');
                            $('#message-body').text('用户名长度在 1~50');
                            $('#alerts').click();

                            return
                        }
                        if (password.length > 18 || password.length < 8) {
                             $('#alertTiele').text('error');
                            $('#message-body').text('密码长度在 8~18');
                            $('#alerts').click();


                            return
                        }
                        if (point < 0 || point > 2147483647) {
                            $('#alertTiele').text('error');
                            $('#message-body').text('点数长度在 0~2147483647');
                            $('#alerts').click();

                            return
                        }
                        if (password !== password1) {
                            $('#alertTiele').text('error');
                            $('#message-body').text('两次密码不一致');
                            $('#alerts').click();

                            return
                        }
                        if(!point){
                            point=1
                        }
                        $.ajax({
                            url: that.urls.edit,
                            type: 'post',
                            data: {
                                'username': username,
                                'password': password,
                                'point': point,
                                'csrf_token': csrf,
                                'user_type':that.user_type
                            },
                            success: (data) => {
                                if (data.code === 200) {
                                    data = data.data;
                                    $('#alertTiele').text('success');
                                    $('#message-body').text('注册成功');
                                    $('#alerts').click();


                                } else {
                                    $('#alertTiele').text('error');
                                    $('#message-body').text(data.message);
                                    $('#alerts').click();

                                }
                            },
                            error: (error) => {
                                $('#alertTiele').text('error');
                                $('#message-body').text('网络错误');
                                $('#alerts').click();

                            }
                        })
        });
        this.changeStatusEle.on('click', function () {
            let ele = $(this);
            let id = parseInt(ele.attr('status'));
            $.ajax({
                url: that.urls.edit,
                type: 'delete',
                data: {
                    'id': id,
                    'user_type':that.user_type,
                    'csrf_token': $('#csrf').attr('content')
                },
                success: (data) => {
                    if (data.code === 200) {
                        swal({
                            type: 'success',
                            text: '修改成功'
                        });
                        let text = ele.text().trim();
                        if (text === '启用') {
                            console.log(ele.text());
                            ele.text('禁用');
                            console.log($(id.toString()));
                            $('#' + id.toString()).replaceWith(
                                '<td id="' + id.toString() + '"><div class="tm-status-circle moving"></div>' +
                                '<b>可用</b></td>')
                        } else {
                            ele.text('启用');
                            $('#' + id.toString()).replaceWith('<td id="' + id.toString() + '"><div class="tm-status-circle cancelled"></div>\n' +
                                '<b>不可用</b></td>')
                        }
                    } else {
                        swal({
                            type: 'error',
                            text: data.data.message
                        })
                    }
                }

            })
        });
        $('#resetsubmit').on('click',function () {
                    let newpwd = $('#resetpwd').val();
                    $.ajax({
                        url: that.urls.reset,
                        type: 'post',
                        data: {
                            user_id: $('#user-id').val(),
                            password: newpwd,
                            csrf_token: $('#csrf').attr('content'),
                            user_type:that.user_type
                        },
                        success: (data) => {
                            if (data.code === 200) {
                                $('#alertTiele').text('success');
                                $('#message-body').text('修改成功');
                                $('#alerts').click();
                            } else {
                                $('#alertTiele').text('error');
                                $('#message-body').text(data.message);
                                $('#alerts').click();

                            }
                        },
                        error: function (error) {
                            $('#alertTiele').text('error');
                                $('#message-body').text('网络错误');
                                $('#alerts').click();
                        }
                    })
        });
        this.changePassworEle.on('click', function (event) {
            let ele = $(this);
            let id = parseInt(ele.attr('user'));
            $('#user-id').val(id);
        });
        this.deleteUserEle.on('click', function () {
            let ele = $(this);
            let id = parseInt(ele.attr('user'));
            swal({
                text: '确认删除该用户？ 删除后不可恢复',
                showCancelButton: true,
                showConfirmButton: true,
                cancelButtonText: '取消',
                confirmButtonText: '确认，我了解'
            }).then(function (result) {
                if (result.value) {
                    $.ajax({
                        url: that.urls.delete_user,
                        type: 'post',
                        data: {
                            user_id: id,
                            csrf_token: $('#csrf').attr('content'),
                            user_type:that.user_type
                        },
                        success: function (data) {
                            if (data.code === 200) {
                                swal({
                                    type: 'success',
                                    text: '删除成功'
                                });
                                $('tr[user="' + id.toString() + '"]').remove()
                            } else {
                                swal({
                                    type: 'error',
                                    text: data.message
                                });
                            }
                        },
                        error: function (error) {
                            swal({
                                type: 'error',
                                text: '网络错误'
                            });
                        }
                    })
                }
            })
        });
        this.editUserEle.on('click', function () {
            let ele = $(this);
            let ids = parseInt(ele.attr('user'));
            $.ajax({
                url:'/cms/edit/one/',
                data:{
                    types:'front',
                    id:ids,
                },
                type:'get',
                success:function (data) {
                    console.log(data);
                    $('#wx_number').val(data.data.max_number);
                    $('#wx_point').val(data.data.point_number);
                    $('#status').val(data.data.status);
                    $('#ids').val(ids);
                }
            })
        });
        $('#edit-submit').on('click',function () {
            let id = $('#ids').val();
            let point = $('#wx_point').val();
            let template_number = $('#wx_number').val();
            let status = $('#status').val();
            $.ajax({
                        url: '/cms/edituser/',
                        type: 'post',
                        data: {
                            csrf_token: $('#csrf').attr('content'),
                            user_id: id,
                            point: point,
                            template_number: template_number,
                            status: status,
                            user_type:that.user_type
                        },
                        success: function (data) {
                            if (data.code === 200) {
                                $('#alertTiele').text('成功');
                                $('#message-body').text('修改成功');
                                $('#alerts').click();
                                let status_ele = $('#' + id.toString());
                                let template_number_ele = $('td[user="' + id.toString() + '"]');
                                let point_ele = $('b[user="' + id.toString() + '"]');
                                if (status === '1') {
                                    status_ele.replaceWith(
                                        '<td id="' + id.toString() + '"><div class="tm-status-circle moving"></div>' +
                                        '<b>可用</b></td>')
                                    $('div[status="'+id.toString()+'"]').text('禁用')
                                } else {
                                    status_ele.replaceWith('<td id="' + id.toString() + '"><div class="tm-status-circle cancelled"></div>\n' +
                                        '                            <b>不可用</b></td>');
                                    $('div[status="'+id.toString()+'"]').text('启用')
                                }
                                template_number_ele.text(template_number);
                                point_ele.text(point)
                            } else {
                                $('#alertTiele').text('失败');
                                $('#message-body').text(data.message);
                                $('#alerts').click();
                            }
                        },
                        error: function () {
                            swal({
                                type: 'error',
                                text: data.message
                            });
                        }
                    })
        })
    }

}

